package com.leyou.auth.service.service;


import com.leyou.auth.common.entity.UserInfo;
import com.leyou.auth.common.utils.JwtUtils;
import com.leyou.auth.service.client.UserClient;
import com.leyou.auth.service.config.JwtProperties;
import com.leyou.user.entity.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthService {


    @Autowired
    private UserClient userClient;
    @Autowired
    private JwtProperties jwtProperties;
    public String authentication(String username, String password) {

        TbUser tbUser = userClient.queryUser(username, password);
        if(tbUser==null){
            return null;//校验失败
        }
        try {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(tbUser.getId());
            userInfo.setUsername(tbUser.getUsername());
            //生成token
            String token = JwtUtils.generateToken(userInfo, jwtProperties.getPrivateKey(), jwtProperties.getExpire());
            return token;//返回token
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
}
